package com.explorati.array;

import java.util.Arrays;

/**
 * 数组的实现 数组：把数据码成一排进行存放。 重要点：索引。索引可以有语义，也可以没有语义 数组最大优点：快速查询。score[2]
 * 数组最好应用于"索引有语义"的情况
 * 
 * @author explorati
 *
 */

// 基于Java的数组，二次封装数组类
public class Array {

	private int[] data;
	// 数组中有效元素的个数
	private int size;

	// 构造函数，传入数组的容量capacity构造Array
	public Array(int capacity) {
		data = new int[capacity];
		size = 0;
	}

	public Array(int[] arr) {
		data = new int[arr.length];
		for (int i = 0; i < data.length; i++) {
			data[i] = arr[i];
		}
	}

	// 无参构造函数，默认数组的容量为10
	public Array() {
		// 10的语义为capacity
		this(10);
	}

	// 获取数组中元素的个数
	public int length() {
		return size;
	}

	// 获取数组的容量
	public int getCapacity() {
		return data.length;
	}

	// 返回数组是否为空
	public boolean isEmpty() {
		return size == 0;
	}

	// 在某位置添加元素
	public void add(int index, int e) {
		if (size == data.length) {
			throw new IllegalArgumentException("AddLast failed. Array is full.");
		}
		// 当index=size时，相当于在末尾添加元素
		if (index < 0 || index > size) {
			throw new IllegalArgumentException("Add failed. Require index >= 0 && index <= size.");
		}
		// size-1相当于此时数组中的最后一个有效元素的位置
		for (int i = size - 1; i >= index; i--) {
			data[i + 1] = data[i];
		}
		data[index] = e;
		size++;
	}

	public void addLast(int e) {
		add(size, e);
	}

	public void addFirst(int e) {
		add(0, e);
	}

	public int remove(int index) {
		if (index < 0 || index >= size) {
			throw new IllegalArgumentException("remove failed, the element doesn't exist");
		}
		int ret = data[index];
		for (int i = index; i < size - 1; i++) {
			data[i] = data[i + 1];
		}
		size--;
		return ret;
	}

	public void removeElement(int e) {
		int index = find(e);
		if (index != -1) {
			remove(index);
		}
	}

	public int removeFirst() {
		return remove(0);
	}

	public int removeLast() {
		return remove(size - 1);
	}

	public void set(int index, int e) {
		if (index < 0 || index >= size) {
			throw new IllegalArgumentException("modify failed, Requare index >= 0 and index < size");
		}
		data[index] = e;
	}

	public int get(int index) {
		if (index < 0 || index >= size) {
			throw new IllegalArgumentException("remove failed, the element doesn't exist");
		}
		return data[index];
	}

	public int getFirst() {
		return get(0);
	}

	public int getLast() {
		return get(size - 1);
	}

	public int[] get() {
		return data;
	}

	public boolean contains(int e) {
		for (int i = 0; i < size; i++) {
			if (data[i] == e)
				return true;
		}
		return false;
	}

	public int find(int e) {
		for (int i = 0; i < size - 1; i++) {
			if (data[i] == e)
				return i;
		}
		return -1;
	}

	public int[] findAll(int e) {
		int[] arr = new int[size];
		for (int i = 0; i < size; i++) {
			if (data[i] == e) {
				arr[i] = i;
			} else {
				arr[i] = -1;
			}
		}
		return arr;
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append("Array size = %d, capacity = %d\n", size, data.length);
		res.append("[ ");
		for (int i = 0; i < size; i++) {
			res.append(data[i]);
			if (i != size - 1) {
				res.append(", ");
			}
		}
		res.append("]");
		return res.toString();
	}

}
